<?php
namespace app\product\admin;
use app\system\admin\Admin;
use app\product\model\Product as ProductModel;
use app\product\model\Category as CategoryModel;
use app\product\model\Brand as BrandModel;
use app\product\model\Supplier as SupplierModel;
use app\product\model\Unit as UnitModel;
use think\Db;
class Index extends Admin
{
    protected $hisiModel = 'Product';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        $suppliers = SupplierModel::all();
        $brands = BrandModel::all();
        $this->assign('suppliers',$suppliers);
        $this->assign('brands',$brands);
        if ($this->request->action() != 'index' && !$this->request->isPost()) {

            $supplier = SupplierModel::getSelect($suppliers);
            $this->assign('supplier', $supplier);

            $category = CategoryModel::getSelect(CategoryModel::getChilds());
            $this->assign('category', $category);

            $brand = BrandModel::getSelect($brands);
            $this->assign('brand', $brand);

            $unit = UnitModel::getSelect(UnitModel::all());
            $this->assign('unit', $unit);
        }
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $brand_id = $this->request->param('brand_id/d', '');
            $supplier_id = $this->request->param('supplier_id/d', '');
            $name = $this->request->param('name', '');
            $coded = $this->request->param('coded', '');
            $unit = UnitModel::column('id,name');
            if(!is_empty($name)){
                $where[] = ['name','like','%'.$name.'%'];
            } 
            if(!is_empty($coded)){
                $where[] = ['coded','like','%'.$coded.'%'];
            } 
            if(!is_empty($brand_id)){
                $where[] = ['brand_id','=',$brand_id];
            } 
            if(!is_empty($supplier_id)){
                $where[] = ['supplier_id','=',$supplier_id];
            } 
            $data['data'] = ProductModel::with(['hasCategory','hasSupplier','hasBrand'])
                            ->where($where)
                            ->page($page)
                            ->limit($limit)
                            ->order('weigh asc,id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['unit_id'] = $unit[$data['data'][$key]['unit_id']];
                $data['data'][$key]['decomposition_id'] = $unit[$data['data'][$key]['decomposition_id']];
            }
            $data['count'] = ProductModel::where($where)->count('id');
            $data['code'] = 0;
            // 品牌筛选使用
            
            // 供应商筛选使用


            return json($data);

        }
        // 分组切换类型 0无需分组切换，1单个分组，2多个分组切换[无链接]，3多个分组切换[有链接]，具体请看application/example/view/layout.html
        $this->assign('hisiTabType', 0);
        // tab切换数据
        // $hisiTabData = [
        //     ['title' => '后台首页', 'url' => 'system/index/index'],
        // ];
        // current 可不传
        // $this->assign('hisiTabData', ['menu' => $hisiTabData, 'current' => 'system/index/index']);
        $products = Db::name('product')->field('id,name')->all();
        $this->assign('products',$products);
        $this->assign('hisiTabData', '');
        return $this->fetch();
    }

    public function import()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $name = $this->request->param('name', '');
            $unit = UnitModel::column('id,name');
            if(!is_empty($name)){
                $where[] = ['name','like','%'.$name.'%'];
            }
            $data['data'] = ProductModel::with(['hasCategory','hasSupplier','hasBrand'])
                            ->where($where)
                            ->order('weigh asc,id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['category'] = $value['has_category']['name'];
                $data['data'][$key]['supplier'] = $value['has_supplier']['name'];
                $data['data'][$key]['brand'] = $value['has_brand']['name'];
                $data['data'][$key]['unit_id'] = $unit[$data['data'][$key]['unit_id']];
                $data['data'][$key]['decomposition_id'] = $unit[$data['data'][$key]['decomposition_id']];
            }
            $data['count'] = ProductModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);
        }
    }
}
